<?php
namespace app\store\controller\transform;

use app\store\controller\Controller;
use \think\Db;
/**
 * 企业微信管理
 * Class User
 * @package app\store\controller
 */
class Wechat extends Controller
{
    protected $wecom_config = [];
    protected $wecomSDK;
    public function _initialize(){
        parent::_initialize();
        $this->wecom_config = config('EnterpriseWeChat');
        $this->wecomSDK = new \EnterpriseWeChat($this->wecom_config['corp_id'],$this->wecom_config['secret']);
    }

    public function refresh(){
        $this->initDepartment();
        return $this->renderSuccess('更新成功', url('transform.wechat/index'));
    }

    public function userinfo($userid){
        return Db::name('wecom_user')->where('userid',$userid)->find();
    }


    public function saveuserinfo($userid,$user=[]){
        Db::name('wecom_user')->where('userid',$userid)->update($user);
        return $this->renderSuccess('更新成功', url('transform.wechat/index'));
    }
    /**
     * 用户列表
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function index()
    {
       //$r = $this->wecomSDK->getUser();

       //$r= $this->wecomSDK->getDepartmentMemberDetails(1);

        $department = Db::name('wecom_departments')->select();
        foreach($department as $v){
            $department_name[$v['id']] = $v['name'];
        }
        $department  = $this->buildTree($department );
        $map = '';
        $id = input('id');
        if($id>0){
            $map = "FIND_IN_SET({$id}, department)";
            $title = $department_name[$id];
        }else{
            $title = $department_name[1];
        }
        $count = Db::name('wecom_user')->where($map)->count();

        $userlist = Db::name('wecom_user')->where($map)->select()->toArray();
        foreach($userlist as $key=>$vo){
            $did = explode(',',$vo['department']);
            $did = $did[0];
            $userlist[$key]['department_name']  = isset($department_name[$did])?$department_name[$did]:'';
        }
        //var_dump($userlist);
        $this->assign('title',$title);
        $this->assign('count',$count);

        $this->assign('department',$department);
        $this->assign('userlist',$userlist);
        return $this->fetch('index');
    }
    protected function initDepartment(){
        $response = $this->wecomSDK->get_department(0);

        if($response['errcode']==0){
            foreach($response['department'] as $department){
                $department['department_leader'] = json_encode($department['department_leader']);
                $r = Db::name('wecom_departments')->where('id','=',$department['id'])->find();
                if(!$r){
                    $department['create_time'] = date('Y-m-d H:i:s');
                    $department['update_time'] = date('Y-m-d H:i:s');
                    Db::name('wecom_departments')->insert($department);
                }else{
                    $department['update_time'] = date('Y-m-d H:i:s');
                    Db::name('wecom_departments')->where('id','=',$department['id'])->update($department);
                }
                $this->initUser($department['id']);
            }
        }
    }

    protected function initUser($department_id=0){
        $response = $this->wecomSDK->get_department_details($department_id);
        if($response['errcode']==0){
            foreach($response['userlist'] as $dept_user){
                $dept_user['department'] = implode(',',$dept_user['department']);
                $r = Db::name('wecom_user')->where('userid','=',$dept_user['userid'])->find();
                if(!$r){
                    $dept_user['create_time'] = date('Y-m-d H:i:s');
                    $dept_user['update_time'] = date('Y-m-d H:i:s');
                    Db::name('wecom_user')->insert($dept_user);
                }else{

                    Db::name('wecom_user')->where('userid','=',$dept_user['userid'])->update([
                        'name'=>$dept_user['name'],
                        'department'=>$dept_user['department'],
                        'update_time'=>date('Y-m-d H:i:s')
                    ]);
                }
            }
        }
    }

    protected function buildTree($arr, $parentId = 0) {  
        $tree = array();  
      
        foreach ($arr as $item) {  
            if ($item['parentid'] == $parentId) {  
                $children = $this->buildTree($arr, $item['id']);  
                if ($children) {  
                    $item['children'] = $children;  
                }  
                $tree[] = $item;  
            }  
        }  
      
        return $tree;  
    }

}


/*

array(3) {
  ["errcode"]=>
  int(0)
  ["errmsg"]=>
  string(46) "ok. WARNING: field `id` expect type `uint32`. "
  ["department_id"]=>
  array(9) {
    [0]=>
    array(3) {
      ["id"]=>
      int(1)
      ["parentid"]=>
      int(0)
      ["order"]=>
      int(100000000)
    }
    [1]=>
    array(3) {
      ["id"]=>
      int(5)
      ["parentid"]=>
      int(1)
      ["order"]=>
      int(99997000)
    }
    [2]=>
    array(3) {
      ["id"]=>
      int(6)
      ["parentid"]=>
      int(5)
      ["order"]=>
      int(100000000)
    }
    [3]=>
    array(3) {
      ["id"]=>
      int(7)
      ["parentid"]=>
      int(5)
      ["order"]=>
      int(99999000)
    }
    [4]=>
    array(3) {
      ["id"]=>
      int(8)
      ["parentid"]=>
      int(1)
      ["order"]=>
      int(99996000)
    }
    [5]=>
    array(3) {
      ["id"]=>
      int(9)
      ["parentid"]=>
      int(8)
      ["order"]=>
      int(100000000)
    }
    [6]=>
    array(3) {
      ["id"]=>
      int(10)
      ["parentid"]=>
      int(8)
      ["order"]=>
      int(99999000)
    }
    [7]=>
    array(3) {
      ["id"]=>
      int(11)
      ["parentid"]=>
      int(1)
      ["order"]=>
      int(99995000)
    }
    [8]=>
    array(3) {
      ["id"]=>
      int(12)
      ["parentid"]=>
      int(1)
      ["order"]=>
      int(99994000)
    }
  }
}
 */